package com.gongjian.database.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gongjian.database.entity.Activity;
import com.gongjian.database.entity.User;
import com.gongjian.utile.CodeEntity;
import com.gongjian.utile.Crud;
import com.gongjian.utile.utile.Hump;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ActivityService {

    @Autowired
    SqlSession sqlSession;
   public String  addActivity(Activity activity){
       sqlSession.insert("addActivity",activity);
       sqlSession.commit();
       return new CodeEntity(200,"ok").toJSONString();
   }

    public String  findAllactivity(int page,int pageSzie,int userId){

        PageHelper.startPage(page, pageSzie);

//      根据分页插件 自行查询出当前页内容
        List<Map<String ,Object>> lists = sqlSession.selectList("findAllactivity",userId);

      //  System.out.println("----lists:"+lists);
//        return sl(lists);
        PageInfo<Map<String ,Object>> pageInfo = new PageInfo<>(lists);
        long count =pageInfo.getTotal();

        CodeEntity codeEntity = new CodeEntity(count,200,"ok");
        String timeFormat = "yyyy-MM-dd HH:mm:ss";
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONStringWithDateFormat(Hump.formatHumpNameForList(lists),timeFormat));  /**list为 List<Map<String, String>>**/

        codeEntity.setData(jsonArray);
//      返回查询记录与code、message一并返回前台
        return codeEntity.toJSONString();

   }



}
